/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bd2;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import java.util.Date;
import java.text.SimpleDateFormat;

import java.util.ArrayList;

/**
 *
 * @author tolkjen
 */
public class KursyTable extends JTable {
    public KursyTable()
    {
        setModel(new KursyTableModel());
        
        setFocusable(false);
        setShowHorizontalLines(true);
        setShowVerticalLines(true);
        setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        setFillsViewportHeight(true);
        getTableHeader().setReorderingAllowed(false);
        getTableHeader().setResizingAllowed(false);
        
        for (int i=0; i<getModel().getColumnCount(); i++)
            getColumnModel().getColumn(i).setCellRenderer(new NormalCellRenderer());
    }
    
    public void addRow(Object[] row)
    {
        KursyTableModel model = (KursyTableModel) getModel();
        model.addRow(row);
        repaint();
    }

    public void removeRow(int index)
    {
        KursyTableModel model = (KursyTableModel) getModel();
        model.removeRow(index);
        repaint();
    }

    public void clear()
    {
        KursyTableModel model = (KursyTableModel) getModel();
        while (model.getRowCount() > 0)
            model.removeRow(0);
        repaint();
    }

    public Object[] getRow(int index)
    {
        KursyTableModel model = (KursyTableModel) getModel();
        return model.row(index);
    }
}

class NormalCellRenderer extends DefaultTableCellRenderer
    {
        private SimpleDateFormat formatter;

        NormalCellRenderer()
        {
            formatter = new SimpleDateFormat("dd/MM/yyyy");
            setHorizontalAlignment(JLabel.CENTER);
        }

        @Override
        public void setValue(Object value)
        {
            if (value instanceof Date)
                setText(formatter.format((Date) value));
            else
                setText(String.valueOf(value));
        }
    }

class KursyTableModel extends AbstractTableModel
{
    private String[] columnNames = {"Id", "Kategoria", "Prowadzący", 
        "Rozpoczęcie", "Zakończenie", "Cena", "L. uczestników"};
    private ArrayList<Object[]> data;

    public KursyTableModel()
    {
        data = new ArrayList<Object[]>();
    }

    public void addRow(Object[] row)
    {
        data.add(row);
    }

    public void removeRow(int index)
    {
        data.remove(index);
    }

    public Object[] row(int index)
    {
        return data.get(index);
    }

    @Override
    public int getColumnCount()
    {
        return columnNames.length;
    }

    @Override
    public int getRowCount()
    {
        return data.size();
    }

    @Override
    public String getColumnName(int col)
    {
        return columnNames[col];
    }

    @Override
    public Object getValueAt(int row, int col)
    {
        return data.get(row)[col];
    }

    @Override
    public Class getColumnClass(int c)
    {
        return Object.class;
    }

    @Override
    public boolean isCellEditable(int row, int col)
    {
        return false;
    }

    @Override
    public void setValueAt(Object value, int row, int col)
    {
        data.get(row)[col] = value;
        fireTableCellUpdated(row, col);
    }
}
